package eduware.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import eduware.util.AWTUtilitiesWrapper;

/**
 * This class represents a splash screen.
 * 
 * @author Florian Bausch
 * 
 */
public class SplashScreen extends JFrame {

	/**
	 * Generated Version ID
	 */
	private static final long serialVersionUID = -4779117967957473425L;

	private JPanel progress, progressbg;
	private JLabel waitLabel, imageLabel;

	/**
	 * This method initialises the splash screen and displays it.
	 */
	public SplashScreen() {
		super("Splash Screen");

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

		this.setLayout(null);

		waitLabel = new JLabel();
		waitLabel.setSize(30, 10);
		waitLabel.setText("0%");
		progress = new JPanel();
		progress.setSize(0, 10);
		progress.setBackground(Color.GREEN);
		progressbg = new JPanel();
		progressbg.setBackground(Color.LIGHT_GRAY);
		progressbg.setLocation(0, 0);

		imageLabel = new JLabel(new ImageIcon("img/dochi.png"),
				SwingConstants.LEFT);
		imageLabel.setSize(300, 290);

		this.getContentPane().add(waitLabel);
		this.getContentPane().add(progress);
		this.getContentPane().add(progressbg);
		this.getContentPane().add(imageLabel);

		this.setUndecorated(true);

		this.setSize(300, 300);
		progress.setLocation(0, 290);

		progressbg.setSize(this.getWidth(), 10);
		progressbg.setLocation(0, 290);
		waitLabel.setLocation(this.getWidth() / 2 - waitLabel.getWidth() / 2,
				290);
		waitLabel.setHorizontalTextPosition(SwingConstants.CENTER);
		imageLabel.setLocation(0, 0);

		this.setResizable(false);

		this.setLocation(screenSize.width / 2 - this.getWidth() / 2,
				screenSize.height / 2 - this.getHeight() / 2);

		this.setAlwaysOnTop(true);
		this.setVisible(true);
	}

	/**
	 * With this method the window is first faded out and then disposed.
	 */
	public void fadeOutAndDispose() {
		if (AWTUtilitiesWrapper.isTranslucencySupported()) {
			float opacity = 1.0f;
			while (opacity > 0) {
				AWTUtilitiesWrapper.setWindowOpacity(this, opacity);
				opacity -= 0.00001f;
			}
		}
		this.dispose();
	}

	public void setProgress(int percent) {
		progress.setSize((percent * this.getWidth()) / 100,
				progress.getHeight());
		waitLabel.setText("" + percent + "%");
		waitLabel.repaint();
	}
}
